package com.maxatime.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.maxatime.dao.ClassroomMapper;
import com.maxatime.exception.PhoenirvanaException;
import com.maxatime.pojo.dto.ClassroomDTO;
import com.maxatime.pojo.entity.Classroom;
import com.maxatime.service.ClassroomService;
import com.maxatime.util.SerialNumberUtil;



@Service
public class ClassroomServiceImpl implements ClassroomService {

	// 日志
	private Logger log = LogManager.getLogger();

	@Autowired
	ClassroomMapper classroomMapper;

	//查询
	@Override
	public List<ClassroomDTO> listData(ClassroomDTO classroomDTO) {
		// TODO Auto-generated method stub
		log.info("begin-listData" + classroomDTO);
		
		// DAO
		Classroom classroom = new Classroom();
		BeanUtils.copyProperties(classroomDTO, classroom);
		
		classroom.setDeleteFlag("0");//查看删除状态为空或者其他的都不显示
		log.info("classroomMapper.listData-req::: " + classroom);
		List<Classroom> list = classroomMapper.listData(classroom);
		log.info("classroomMapper.listData-resp::: " + list);
		
		List<ClassroomDTO> listDTO = new ArrayList<ClassroomDTO>();
		
		for(int i = 0; i < list.size(); i++){
			ClassroomDTO crDTO = new ClassroomDTO();
			BeanUtils.copyProperties(list.get(i), crDTO);
			listDTO.add(crDTO);
		}
		
		log.info("service-resp::: " + listDTO);
		// return list
		return listDTO;
	}

	@Override
	public ClassroomDTO findByClassroomId(String classroomId) {
		// TODO Auto-generated method stub
		log.info("begin-findByClassroomId:::"+classroomId);
		log.info("classroomMapper.selectByClassroomId-req::: "+classroomId);
		Classroom classroom = classroomMapper.selectByClassroomId(classroomId);
		log.info("classroomMapper.selectByClassroomId-resp::: " + classroom);
		ClassroomDTO classroomDTO = new ClassroomDTO();
		BeanUtils.copyProperties(classroom, classroomDTO);
		log.info("service-resp::: " + classroomDTO);
		return classroomDTO;
	}
	
	//修改
	@Override
	public int updateData(ClassroomDTO record) {
		log.info("updateData-req::: " + record);
		
		Classroom classroom = new Classroom();
		BeanUtils.copyProperties(record, classroom);
		
		int iRet = classroomMapper.updateByPrimaryKeySelective(classroom);
		return iRet;
		
	}

	
	//删除
	@Override
	public int deleteByPrimaryKey(Integer pkId) {
		// TODO Auto-generated method stub
		int iRet = classroomMapper.deleteByPrimaryKey(pkId);
		return iRet;
	}

	@Override
	public int saveData(ClassroomDTO record) throws PhoenirvanaException {
		// TODO Auto-generated method stub
		//新增
			// TODO Auto-generated method stub
			Classroom classroom = new Classroom();
			BeanUtils.copyProperties(record, classroom);
			
			// classroomId // 雪花算法
			String classroomId = "class" + SerialNumberUtil.getRandomNum();
			classroom.setClassroomId(classroomId);
			// delete_flag
			classroom.setDeleteFlag("0");
			
			// use_status 使用状态 0-使用 1-未使用 2-维修中
			
			// 创建时间，修改时间
			Date d = new Date();
			classroom.setCreateTime(d);
			classroom.setUpdateTime(d);
			
			
			int iRet = 0;
			try {
				iRet = classroomMapper.insertSelective(classroom);
			} catch (Exception e) {
				// log.warn("");
				log.info("出现异常");
				// log.error("");
				
				Throwable cause = e.getCause();
				if(cause instanceof java.sql.SQLIntegrityConstraintViolationException) {
					String errMsg = ((java.sql.SQLIntegrityConstraintViolationException)cause).getMessage();
					throw new PhoenirvanaException("0011",e.getMessage());
					/**
					if(StringUtils.isNotBlank(errMsg) && errMsg.indexOf("ux_aircraft_model") != -1) {
						// return R.failed().setMsg("机型（"+aircraft.getAircraftModel()+"）重复");
					}else{
						// return R.failed().setMsg("新增机型失败");
					}
					*/
				}else {
					throw new PhoenirvanaException("9999","交易失败");
				}
			}finally{
				
			}
			log.info("classroomMapper.insertSelective-resp::: "+iRet);
			return iRet;
	}

}